<script>
export default {
  name: 'CountBox',
  props: {
    value: {
      type: Number,
      default: 1
    }
  },
  methods: {
    handleAdd () {
      this.$emit('input', this.value + 1)
    },
    handleSub () {
      if (this.value > 1) {
        this.$emit('input', this.value - 1)
      }
    },
    handleChange (e) {
      const newValue = +e.target.value
      if (!isNaN(newValue) && newValue > 0) {
        this.$emit('input', newValue)
        console.log(newValue)
      } else {
        this.$emit('input', 1) // 如果输入无效，重置为1
      }
    }
  }
}
</script>

<template>
<div class="count-box">
  <button class="minus" @click="handleSub">-</button>
  <input class="inp" type="text" :value="value" @change="handleChange($event)">
  <button class="add" @click="handleAdd">+</button>
</div>
</template>

<style scoped lang="less">
.count-box {
  width: 110px;
  display: flex;
  .add, .minus {
    width: 30px;
    height: 30px;
    outline: none;
    border: none;
    background-color: #39a9ed;
  }
  .inp {
    width:40px;
    height: 30px;
    outline:none;
    margin: 0 5px;
    background-color: #39a9ed;
    border: none;
    text-align: center;
  }
}
</style>
